using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Odb_base.Interop;
using Odb_base.Interop.Interfaces;

namespace Odb_base.Controls
{
  public class FileAddedEventArgs : EventArgs
  {
    public readonly string FileSystemPath;
    public readonly FileInfo FileInfo;

    public FileAddedEventArgs(string fileSystemName, FileInfo fileInfo)
    {
      FileSystemPath = fileSystemName;
      FileInfo = fileInfo;
    }
  }

  public class DirectoryAddedEventArgs : EventArgs
  {
    public readonly string FileSystemPath;
    public readonly DirectoryInfo FileInfo;

    public DirectoryAddedEventArgs(string fileSystemName, DirectoryInfo directoryInfo)
    {
      FileSystemPath = fileSystemName;
      FileInfo = directoryInfo;
    }
  }

  public class Burner
  {
    private MsftFileSystemImage _fileImage = null;
    private MsftDiscRecorder2 _currentRecorder = null;

    #region Events

    public delegate void FileAddedEventHandler(object sender, FileAddedEventArgs e);

    public event FileAddedEventHandler FileAdded;

    public void OnFileAdded(FileAddedEventArgs e)
    {
      if (FileAdded != null)
      {
        foreach (FileAddedEventHandler del in FileAdded.GetInvocationList())
        {
          del.Invoke(this, e);
        }
      }
    }

    public delegate void DirectoryAddedEventHandler(object sender, DirectoryAddedEventArgs e);

    public event DirectoryAddedEventHandler DirectoryAdded;

    public void OnDirectoryAdded(DirectoryAddedEventArgs e)
    {
      if (DirectoryAdded != null)
      {
        foreach (DirectoryAddedEventHandler del in DirectoryAdded.GetInvocationList())
        {
          del.Invoke(this, e);
        }
      }
    }

    #endregion Events

    public Burner(string recorderUniqueId)
    {
      try
      {
        _currentRecorder = new MsftDiscRecorder2();
        _currentRecorder.InitializeDiscRecorder(recorderUniqueId);
      }
      catch (COMException)
      {
        _currentRecorder = null;
      }
      _fileImage = new MsftFileSystemImage();
    }

    public static string[] GetRecorderIds()
    {
      MsftDiscMaster2 discMaster = new MsftDiscMaster2();
      string[] ids = new string[discMaster.Count];
      if (discMaster.IsSupportedEnvironment)
      {
        for (int i = 0; i < discMaster.Count; i++)
        {
          ids[i] = discMaster[i];
        }
      }
      return ids;
    }

    public bool AddNewDirectory(string name)
    {
      return false;
    }
    private IStream CreateIStream(string fullPath)
    {
      FileStream fileStream = new FileStream(fullPath,FileMode.Open);
      StreamConverter converter = new StreamConverter(fileStream);
      return converter;
     
    }

    public bool AddFile(string fullPath, bool addToRoot)
    {
      
  
      if (File.Exists(fullPath))
      {
        FileInfo fileInfo = new FileInfo(fullPath);
        OnFileAdded(new FileAddedEventArgs(Path.GetFileName(fullPath),fileInfo));
        if(addToRoot)
        {
          IFsiDirectoryItem rootItem = _fileImage.Root;
          rootItem.AddDirectory("Ordner");
          rootItem.AddFile(Path.GetFileName(fullPath),CreateIStream(fullPath));
          foreach (object o in rootItem)
          {
            IFsiDirectoryItem obj = (IFsiDirectoryItem) o;
    
            Debug.WriteLine(obj.Name);
          }
          
        }
        return true;
      }
      else
      {
        return false;
      }
      
    }
  }
}